/* common function */
function hasClass(elements, cName) {
    return !!elements.className.match(new RegExp("(\\s|^)" + cName + "(\\s|$)"));
};

function addClass(elements, cName) {
    if (!hasClass(elements, cName)) {
        elements.className += " " + cName;
    };
};

function removeClass(elements, cName) {
    if (hasClass(elements, cName)) {
        elements.className = elements.className.replace(new RegExp("(\\s|^)" + cName + "(\\s|$)"), " ");
    };
};
var timer;
var zwfyReplayFn;
var zwfyUpdateRecord;
var flag = false;

//platform judge and share
var isWeChat = false;
var isQQ = false;
var isWeibo = false;
(function checkShare() {
    var ua = navigator.userAgent.toLowerCase();
    if (/micromessenger/.test(ua)) {
        isWeChat = true;
    } else if (/weibo/.test(ua)) {
        isWeibo = true;
    } else if (/qq/.test(ua)) {
        isQQ = true;
    }
})();

/*服务器游戏记录获取*/
var playerRecord = {};
playerRecord = window.parent.playerRecord;
if(playerRecord){
    var reScore = playerRecord.reScore;
    var bestRecord = playerRecord.bestRecord;
    var localStorage_itwlw = playerRecord.localStorage_itwlw;
}else{
    var reScore = null;
    var bestRecord = null;
    var localStorage_itwlw = null;
}

//var bestRecord = playerRecord.bestRecord;

console.log(reScore,bestRecord,localStorage_itwlw);

(function(){
    var data = {};
    data['method'] = 'getRecord';
    window.parent.postMessage(data, "*");
})()

/*修改分享标题  OK*/
function changeShare() {
    //shareConfig.title = arguments[0];
    //shareData.title = arguments[0];
    var data = {};
    data['method'] = 'changeShare';
    data['title'] = arguments[0];
    window.parent.postMessage(data, "*");
    //console.log('changeShare in');
}

/*更新游戏记录接口*/
function updateRecord(score, Data, fn) {

alert(Data);
    var data = {};
    data.method = 'updateRecord';
    data.score = score;
    data.data = Data;
    zwfyUpdateRecord = fn;
    data.fn = fn ? 1 : 0;
    window.parent.postMessage(data, "*");
    //console.log('updateRecord send msg');
}

/*更新游戏记录额外数据接口 OK*/
function updateStorage(Data) {
  
alert(Data);
  var data = {};
    data.method = 'updateStorage';
    data.data = Data;
    window.parent.postMessage(data, "*");
/*    console.log('updateStorage send msg');*/
}

/*游戏结束调用 OK*/
function commonGameOver(score, propor, records, fn) {
 alert(score);
    var  data= {};
    data.method = 'commonGameOver';
    data.score= score;
    data.propor = propor;
    data.records = records;
    data.rand=tinygameRand(score);
    zwfyReplayFn = fn;
    data.fn = fn ? 1 : 0;
    window.parent.postMessage(data, "*");
/*    console.log('commonGameOver')*/
}

/*更多游戏接口 OK*/
function commonMoreGame() {
    var data = {};
    data['method'] = 'commonMoreGame';
    window.parent.postMessage(data, "*");
/*    console.log('commonMoreGame')*/
}

/*游戏排行榜接口 OK*/
function commonRank() {
    var data = {};
    data['method'] = 'commonRank';
    window.parent.postMessage(data, "*");
/*    console.log('commonRank')*/
}

/*打开侧滑菜单 OK*/
function commonOpenMenu(event){
    var data = {};
    data['method'] = 'commonOpenMenu';
    window.parent.postMessage(data, "*");
/*    console.log('commonOpenMenu')*/
    event&&event.preventDefault();
    return true;
}

/*打开非微信平台分享提示 OK*/
function showPlatformShare() {
    var data = {};
    data['method'] = 'showPlatformShare';
    window.parent.postMessage(data, "*");
/*    console.log('showPlatformShare in')*/
    event&&event.preventDefault();
    return true;
}
/*隐藏非微信平台分享提示 OK*/
function hidePlatformShare() {
    var data = {};
    data['method'] = 'hidePlatformShare';
    window.parent.postMessage(data, "*");
/*    console.log('hidePlatformShare in')*/
    event&&event.preventDefault();
    return true;
}
/*监听再玩一次消息*/
window.addEventListener('message',function(e){
    switch(e.data.method){
        case 'replay':
            if(zwfyReplayFn && typeof(zwfyReplayFn)=="function"){
                zwfyReplayFn();
            }
            window.focus();
            break;
        case 'sendScore':
            zwfySendScore(e.data);
    }

/*    console.log(e.data);*/
});

function zwfySendScore(Data){
/*    console.log("get Data OK!"+Data.reScore+" "+Data.bestRecord+" " +Data.localStorage_itwlw)*/
    Score = Data.Score;
    bestRecord = Data.bestRecord;
    localStorage_itwlw = Data.localStorage_itwlw;
}

setTimeout(function(){
    window.onblur = function(){
        window.focus();
    }
},1000)


function tinygameRand(num){
    //var code = 32;
    var code = num;
    var a = null;
    var c = [];
    var float = null;
    if(code.toString().split('.')[1]){
        float = code.toString().split('.')[1].length;
        code = Math.pow(10,float)*code;
    }else{
        float = null;
    }
    var key = Math.floor(Math.random()*10+code)+1;
    var xiaoyan =Math.floor( Math.random()*20 +1);
    var count = key*xiaoyan + code;
    var d = [];
    var a1 = Math.random()*0.2+0.25;
    var a2 = Math.random()*0.2+0.25;
    var a3 = 1-a1-a2;
    d.push ( Math.floor(Math.pow(count,a1)));
    d.push ( Math.floor(Math.pow(count,a2)));
    d.push (Math.floor(Math.pow(count,a3)));
    d.push (( count - d[0]*d[1]*d[2]));
    d.push(key);
    d.push(xiaoyan);
    d.push(count%d[5]);
    /*    console.log(count,d);*/
    if(float){
        float = xiaoyan*float;
        d.push(float.toString());
    }
    for(var i=0;i< d.length;i++){
        var b = '';
        var index = 0;
        for(var j= 0,len=d[i].toString().length*2+1;j<len;j++){
            if(j%2){
                b+=(d[i].toString().charAt(index));
                index++;
            }else{
                b+=(String.fromCharCode(Math.floor(Math.random()*23+65)));
            }
        }
        c.push(b);
    }
    c = c.join(String.fromCharCode(90));
    if(   count%d[5]  !==d[6]){
        console.log(count%d[5]);
        return  encode(num);
    }
    else{
        return c;
    }
}
